package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.UserLog;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.UserLogService;
import com.symone.webcms.util.DateUtil;
import com.xiaoleilu.hutool.lang.Validator;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("api/log")
@Api(value="api/sysLog", description="系统日志管理")
public class SysLogController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(SysLogController.class);

    @Autowired
    private UserLogService logService;

    @RequiresPermissions("chart:sysLog:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(String month, String userName, Integer pageSize, Integer pageNum) {

        Wrapper<UserLog> ew = new EntityWrapper<UserLog>();
        ew.where("deleted=0");

        if(Validator.isEmpty(month)) {
            ew.and().where("create_time>{0}", DateUtil.getMonthFirstDay(new Date()));
            ew.and().where("create_time<{0}", DateUtil.getMonthLastDay(new Date()));
        } else {
            ew.and().where("DATE_FORMAT( create_time, '%Y-%m' )={0}",month);
        }

        if( false == Validator.isEmpty(userName)) {
            ew.and().like("user_name",userName);
        }
        ew.orderBy("create_time",false);
        //logger.debug("SQL:"+ew.originalSql());
        if(pageNum == null){
            pageNum = 1;
        }

        if(pageSize == null){
            pageSize = 10;
        }

        Page<UserLog> page = new Page<UserLog>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = logService.findPage(page,ew);

        return ResultMessage.success("获取Log分页列表",page.getRecords(),page.getTotal());
    }

    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }


    /**
     * @description : 清除登录日志记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-11-18
     */
    @RequiresPermissions("chart:sysLog:index")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = logService.deleteAll();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有日志记录被清除！");
        }
    }

}
